﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Celeriq.Common;
using PJB.FacetedNavigation;

namespace PJBWebsite.UserControls
{
    public partial class GeoSearchControl : System.Web.UI.UserControl
    {
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            cboDistance.Items.Add(new ListItem("10 miles", "10"));
            cboDistance.Items.Add(new ListItem("20 miles", "20"));
            cboDistance.Items.Add(new ListItem("30 miles", "30"));
            cboDistance.Items.Add(new ListItem("40 miles", "40"));
            cboDistance.Items.Add(new ListItem("50 miles", "50"));
            cboDistance.Items.Add(new ListItem("100 miles", "100"));
            cboDistance.SelectedIndex = 1;

            cmdGo.Click += cmdGo_Click;
        }

        private void cmdGo_Click(object sender, EventArgs e)
        {
            var client = new geolocationservice.GeoLocationServiceClient();
            var info = client.GetZip(txtZip.Text);
            if (info != null)
            {
                var url = new ListingQuery("/results.aspx");
                var radius = Convert.ToInt32(cboDistance.SelectedValue);
                url.FieldFilters.Add(new FieldFilterLocation() { Latitude = info.Latitude, Longitude = info.Longitude, Radius = radius });
                url.NonParsedFieldList.Add(new NamedItem() { Key = "zip", Value = txtZip.Text });
                url.NonParsedFieldList.Add(new NamedItem() { Key = "rd", Value = cboDistance.SelectedValue });
                this.Response.Redirect(url.ToString());
            }
            else
            {
                //Not found
            }

        }

    }
}